//
//  SinaAccountModel.m
//  oc微博
//
//  Created by 安陆与 on 16/1/18.
//  Copyright © 2016年 安陆与. All rights reserved.
//

#import "SinaAccountModel.h"

@interface  SinaAccountModel()<NSCoding>



@end


@implementation SinaAccountModel

//+(NSString *)description{
//
//    [super init];
//    NSLog(@"token:%@----过期日期:%@----uid:%@----用户名:%@----头像:%@",);
//    
//}


/**
 *  归档函数
 *
 */
-(void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeObject:self.expires_in forKey:@"expires_in"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.screen_name forKey:@"screen_name"];
    [aCoder encodeObject:self.profile_image_url forKey:@"profile_image_url"];
}

/**
 *解档方法
 */
-(instancetype)initWithCoder:(NSCoder *)aDecoder{

    self = [super init];
    
    self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
    self.expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
    self.uid = [aDecoder decodeObjectForKey:@"uid"];
    self.screen_name = [aDecoder decodeObjectForKey:@"screen_name"];
    self.profile_image_url = [aDecoder decodeObjectForKey:@"profile_image_url"];

    return self;
}

//保存文件的方法
-(void)saveInfo{

    //获取沙盒路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *filePath = [path stringByAppendingPathComponent:@"account.info"];
    NSLog(@"%@",filePath);
    
    [NSKeyedArchiver archiveRootObject:self toFile:filePath];
}

//读取文件的方法

+(instancetype)readInfo{
    //获取沙盒路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *filePath = [path stringByAppendingPathComponent:@"account.info"];
//    NSLog(@"%@",filePath);

    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

}

+(SinaAccountModel *)accountWithDict:(NSDictionary *)dict{

    NSArray *propreties = @[@"access_token",@"expires_in",@"uid"];
    
    SinaAccountModel *account = [[self alloc]init];
    
    for (NSString *key in propreties) {
        if (key != nil) {
            [account setValue:dict[key] forKey:key];
        }
    }
    return account;
}
@end
